package javaproject;

import javax.media.j3d.LineArray;
import javax.media.j3d.QuadArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Appearance;

abstract class BasicBlock extends MyPrimitive{
    public float alpha;
    protected float offset;   //方塊面內縮值，減少反鋸齒錯誤
    protected float[][] verts =
    {
        {-1,-1,-1},
        {1,-1,-1},
        {-1,1,-1},
        {1,1,-1},
        {-1,-1,1},
        {1,-1,1},
        {-1,1,1},
        {1,1,1}
    };

    BasicBlock(float[] dim, double[] pos, Appearance app, float alpha, float offset)
    {
        super(pos[0], pos[1], pos[2], app);
        //重設基本方塊大小
        for(int i = 0; i < 8; i++)
            for(int j = 0; j < 3; j++)
                verts[i][j] *= dim[j];

        this.alpha = alpha;
        this.offset = offset;
        transG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        //只設定位置，不要用moveTo，可能會呼叫到底層的moveTo
        translate.set(new Vector3d(x,y,z));
        transG.setTransform(translate);
    }
}
class Block extends BasicBlock{
    Block(float[] dim, double[] pos, Appearance app, float alpha, float offset)
    {
        super(dim,pos,app,alpha,offset);

        //設定方塊座標
        float[] cubeVertices =
        {
            //第一個是底部
            verts[0][0]+offset,verts[0][1]+offset,verts[0][2]+offset,    verts[2][0]+offset,verts[2][1]-offset,verts[2][2]+offset,    verts[3][0]-offset,verts[3][1]-offset,verts[3][2]+offset,    verts[1][0]-offset,verts[1][1]+offset,verts[1][2]+offset,
            verts[0][0]+offset,verts[0][1]+offset,verts[0][2]+offset,    verts[1][0]-offset,verts[1][1]+offset,verts[1][2]+offset,    verts[5][0]-offset,verts[5][1]+offset,verts[5][2]-offset,    verts[4][0]+offset,verts[4][1]+offset,verts[4][2]-offset,
            verts[0][0]+offset,verts[0][1]+offset,verts[0][2]+offset,    verts[4][0]+offset,verts[4][1]+offset,verts[4][2]-offset,    verts[6][0]+offset,verts[6][1]-offset,verts[6][2]-offset,    verts[2][0]+offset,verts[2][1]-offset,verts[2][2]+offset,
            verts[4][0]+offset,verts[4][1]+offset,verts[4][2]-offset,    verts[5][0]-offset,verts[5][1]+offset,verts[5][2]-offset,    verts[7][0]-offset,verts[7][1]-offset,verts[7][2]-offset,    verts[6][0]+offset,verts[6][1]-offset,verts[6][2]-offset,
            verts[1][0]-offset,verts[1][1]+offset,verts[1][2]+offset,    verts[3][0]-offset,verts[3][1]-offset,verts[3][2]+offset,    verts[7][0]-offset,verts[7][1]-offset,verts[7][2]-offset,    verts[5][0]-offset,verts[5][1]+offset,verts[5][2]-offset,
            verts[3][0]-offset,verts[3][1]-offset,verts[3][2]-offset,    verts[2][0]+offset,verts[2][1]-offset,verts[2][2]+offset,    verts[6][0]+offset,verts[6][1]-offset,verts[6][2]-offset,    verts[7][0]-offset,verts[7][1]-offset,verts[7][2]-offset
        };

        QuadArray cube = new QuadArray(24, QuadArray.COORDINATES | QuadArray.COLOR_4);
        Color3f intrinsicColor = new Color3f();
        app.getColoringAttributes().getColor(intrinsicColor);
        float[] boxColor = new float[96];
        for(int i = 0; i < 24; i++)
        {
            boxColor[i*4] = intrinsicColor.get().getRed();
            boxColor[i*4+1] = intrinsicColor.get().getGreen();
            boxColor[i*4+2] = intrinsicColor.get().getBlue();
            boxColor[i*4+3] = alpha;
        }
        cube.setCoordinates(0, cubeVertices);
        cube.setColors(0, boxColor);
        shape = new Shape3D(cube, app);
        transG.addChild(shape);
    }
}
class BlockFrame extends BasicBlock{
    BlockFrame(float[] dim, double[] pos, Appearance app, float alpha)
    {
        super(dim, pos, app, alpha, 0);

        //設定邊線座標
        float[] lineVertices =
        {
            verts[0][0],verts[0][1],verts[0][2],     verts[1][0],verts[1][1],verts[1][2],
            verts[0][0],verts[0][1],verts[0][2],     verts[2][0],verts[2][1],verts[2][2],
            verts[2][0],verts[2][1],verts[2][2],     verts[3][0],verts[3][1],verts[3][2],
            verts[1][0],verts[1][1],verts[1][2],     verts[3][0],verts[3][1],verts[3][2],
            verts[0][0],verts[0][1],verts[0][2],     verts[4][0],verts[4][1],verts[4][2],
            verts[2][0],verts[2][1],verts[2][2],     verts[6][0],verts[6][1],verts[6][2],
            verts[1][0],verts[1][1],verts[1][2],     verts[5][0],verts[5][1],verts[5][2],
            verts[3][0],verts[3][1],verts[3][2],     verts[7][0],verts[7][1],verts[7][2],
            verts[4][0],verts[4][1],verts[4][2],     verts[6][0],verts[6][1],verts[6][2],
            verts[4][0],verts[4][1],verts[4][2],     verts[5][0],verts[5][1],verts[5][2],
            verts[6][0],verts[6][1],verts[6][2],     verts[7][0],verts[7][1],verts[7][2],
            verts[5][0],verts[5][1],verts[5][2],     verts[7][0],verts[7][1],verts[7][2]
        };

        LineArray line = new LineArray(24, LineArray.COORDINATES | LineArray.COLOR_4);
        Color3f intrinsicColor = new Color3f();
        app.getColoringAttributes().getColor(intrinsicColor);
        float[] lineColor = new float[96];
        for(int i = 0; i < 24; i++)
        {
            lineColor[i*4] = intrinsicColor.get().getRed();
            lineColor[i*4+1] = intrinsicColor.get().getGreen();
            lineColor[i*4+2] = intrinsicColor.get().getBlue();
            lineColor[i*4+3] = alpha;
        }
        line.setCoordinates(0, lineVertices);
        line.setColors(0, lineColor);
        shape = new Shape3D(line, app);
        transG.addChild(shape);
    }
}